package cn.doit.scala.day04.Friends

import scala.collection.immutable
import scala.io.Source

/**
 * @DATE 2021/12/29/20:02
 * @Author MDK
 * @Version 2021.2.2
 * */
object C01_Friends02 {
  def main(args: Array[String]): Unit = {
    val lines = Source.fromFile("data/friends.txt").getLines()
//    lines.foreach(println)
    //将数据封装在Map集合中
//    Map[String,Array[String]] 考虑下用List的情况 Map[String,List[String]]
    val list: List[(String, List[String])] = lines.map(line => {
      val arr = line.split(":")
      val name = arr(0)
      val fri = arr(1).split(",")
      (name, fri.toList)
    }).toList
//   显示任意对象的好友信息
//    tuples.foreach(tp=>println((tp._1, tp._2.toList)))

    //任意两个人的好友,两层循环  守卫模式收集数据
    val tuples: immutable.Seq[(String, List[String])] = for (i <- 0 to list.length - 1; j <- i + 1 to list.length - 1) yield {
      val name = list(i)._1
      val fri = list(i)._2
      val name2 = list(j)._1
      val fri2 = list(j)._2
      val res = fri.intersect(fri2)
      (s"$name 和 $name2 的共同好友", res)
    }
    tuples.filter(_._2.length>0).foreach(println)
  }
}
